﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;


namespace VitCode.Web.BackStageManagement
{
    public partial class MenuList : VcPage
    {
        private string functionString = "";
        readonly CastleAccess m_Acc = new CastleAccess();
        readonly BaseDataManager m_Help = new BaseDataManager();
        readonly PermissionManager m_PermissionManger = new PermissionManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                if(String.IsNullOrEmpty(GenericContext.CurrUserCode))
                {
                  Response.Redirect("../Default.aspx");
                } 
                ShowTree();
            }
        }

        public void ShowTree()
        {
            TreeView_Menu.Nodes.Clear();
            TreeView_Menu.ToolTip = "";
            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "Images/calendar.gif";
            node_0.Text = "功能表管理" + "<a herf='#' onclick='return false;'></a>";
            node_0.Value = "0";
            node_0.ToolTip = "";
            node_0.Text += "&nbsp;&nbsp;<a target='_self' href='MenuInsert.aspx?id=" + publ.GetUrlToSend(node_0.Value.Trim()) +
                "'><img style='width:16px;height:16px' src='Images/msnadd.gif' alt='新增子功能表'/></a>";

            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            TreeView_Menu.Nodes.Add(node_0);
            ViewState["ds"] = m_Acc.GetDataSetBySql("SELECT m.* FROM VitCode_Castle_Menu m    WHERE 1 = 1    and m.ParentId is null order by SortKey ");
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                Node.Text = Row["Name"].ToString().Trim();
                Node.Value = Row["id"].ToString().Trim();
                Node.ToolTip = "";
                Node.ImageUrl = "Images/chart_bar.gif";

                Node.Text += "&nbsp;&nbsp;<a target='_self' href='MenuInsert.aspx?id=" +
                    publ.GetUrlToSend(Node.Value.Trim()) + "'><img style='width:16px;height:16px' src='Images/msnadd.gif' alt='新增子功能表'/></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='MenuUpdate.aspx?id=" +
                    publ.GetUrlToSend(Node.Value.Trim()) + "'><img style='width:16px;height:16px' src='Images/edit.gif' alt='編輯'/></a>";
                node_0.ChildNodes.Add(Node);
                AddTree(Node.Value, Node,true);
            }

        }

        public void AddTree(String ParentID, TreeNode pNode,bool isCanAdd)
        {
            IList objParentList = m_Acc.GetObjectList(new VitCode.Castle.Entities.Menu().GetType().Name, "Id", ParentID.Trim());
            if (objParentList != null && objParentList.Count > 0)
            {
                var objParentMenu = (VitCode.Castle.Entities.Menu)objParentList[0];
                IList objList = m_Acc.Session.CreateQuery("FROM Menu where Parent='" +
                                        objParentMenu.Id.ToString().Trim() + "' order by SortKey").List();
                if (objList != null && objList.Count > 0)
                {
                    foreach (VitCode.Castle.Entities.Menu currentMenu in objList)
                    {
                        TreeNode Node = new TreeNode();
                        Node.Value = currentMenu.Id.ToString();
                        Node.Text = currentMenu.Name.Trim() + "<a herf='#'  onclick='return false;'></a>";
                        Node.ToolTip = "";
                        if (currentMenu.UrlPath.Length != 0)
                        {
                            Node.Text += "<font style='font-size: 11px' color='#808080'>[" + currentMenu.UrlPath.Trim() + "]</font> ";
                        }
                        if(isCanAdd)
                        {
                        
                        }
                        Node.Text += "&nbsp;&nbsp;<a target='_self' href='MenuUpdate.aspx?id=" +
                            publ.GetUrlToSend(Node.Value.Trim()) + "'><img style='width:16px;height:16px' src='Images/edit.gif' alt='編輯'/></a>";

                        pNode.ChildNodes.Add(Node);
                        
                        Node.ImageUrl = "../../Images/notes.gif";
                        AddTree(currentMenu.Id.ToString().Trim(), Node,false);    //再次遞迴
                    }
                }
            }

        }

  

       
       /// <summary>
        /// 調用自身函數迴圈遍曆TreeView
       /// </summary>
        /// <param name="node"></param>
        /// <returns>返回FunctionString字串</returns>
        public string GetTreeCheck(TreeNode node)
        {
            foreach (TreeNode treenode in node.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    string vb = treenode.Text;
                    functionString += ", " + va + "";
                }
                GetTreeCheck(treenode);

            }
            return functionString;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string menuCodeAll = GetTreeCheck(TreeView_Menu.Nodes[0]);
            if (string.IsNullOrEmpty(menuCodeAll))
            {
                Alert.MessageBox("請選擇要刪除的功能表!");
                return;
            }
            string[] ss = menuCodeAll.Split(',');
            bool access = false;
            for (int i = 0; i < ss.Length; i++)
            {
                if (!string.IsNullOrEmpty(ss[i]))
                {

                    access = m_Help.DeleteObjById(new VitCode.Castle.Entities.Menu(), ss[i].Trim());
                }

            }
            if (access)
            {
                Alert.MessageBox("刪除成功!");
            }

            ShowTree();
        } 


    }
}
